#!/bin/bash

set -e

echo "###############################################"
echo "Please ensure your OS is CentOS7 64 bits"
echo "Please ensure your machine has full network connection and internet access"
echo "Please ensure run this script with root user"

# Check hostname, Mac addr and product_uuid
echo "###############################################"
echo "Please check hostname as below:"
uname -a

echo "###############################################"
echo "Please check Mac addr and product_uuid as below:"
ip link
sudo cat /sys/class/dmi/id/product_uuid

# Stop firewalld
echo "###############################################"
echo "Stop firewalld"
systemctl stop firewalld
systemctl disable firewalld

# Disable SELinux
echo "###############################################"
echo "Disable SELinux"
setenforce 0
cp -p /etc/selinux/config /etc/selinux/config.bak$(date '+%Y%m%d%H%M%S')
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

# Turn off Swap
echo "###############################################"
echo "Turn off Swap"
swapoff -a
cp -p /etc/fstab /etc/fstab.bak$(date '+%Y%m%d%H%M%S')
sed -i "s/\/dev\/mapper\/rhel-swap/\#\/dev\/mapper\/rhel-swap/g" /etc/fstab
sed -i "s/\/dev\/mapper\/centos-swap/\#\/dev\/mapper\/centos-swap/g" /etc/fstab
mount -a
free -m
cat /proc/swaps

echo "###############################################"
echo "Optimizing File Open Number"
ulimit -n
ulimit -a

cat >> /etc/security/limits.conf <<EOF
* soft nofile 1024000
* hard nofile 1024000
hadoop - nofile 1024000
hadoop - nproc 1024000
EOF

echo "###############################################"
echo "User process constraints"

cat >> /etc/security/limits.d/20-nproc.conf <<EOF
* soft nproc 40960
root soft nproc unlimited
EOF

echo "###############################################"
echo "Kernel optimization"
mv /usr/lib/sysctl.d/00-system.conf /usr/lib/sysctl.d/00-system.conf_bak$(date '+%Y%m%d%H%M%S')
cp 00-system.conf  /usr/lib/sysctl.d/00-system.conf


# Use Aliyun Yum source
echo "###############################################"
echo "Use Aliyun Yum source"
./use_aliyun_yum_source.sh